﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Diagnostics;
using PdfSharp;
using PdfSharp.Drawing;
using PdfSharp.Pdf;
using PdfSharp.Pdf.IO;
using MigraDoc;
using MigraDoc.DocumentObjectModel;
using MigraDoc.RtfRendering;
using MigraDoc.DocumentObjectModel.Tables;
using MigraDoc.DocumentObjectModel.Shapes;
using MigraDoc.Rendering;
using SBS;

namespace Data
{
    class Printer
    {
        public static void VisFaktura(Ordre o)
        {
            Document d = new Document();

            OpretSide(d, o);
            MigraDoc.DocumentObjectModel.IO.DdlWriter.WriteToFile(d, "MigraDoc.mdddl");

            PdfDocumentRenderer tempPDF = new PdfDocumentRenderer(true, PdfSharp.Pdf.PdfFontEmbedding.Always);
            tempPDF.Document = d;
            tempPDF.RenderDocument();

            string filnavn = "Temp_Faktura.pdf";
            tempPDF.PdfDocument.Save(filnavn);


            Process.Start(filnavn);

        }
        public static void LavFaktura(string mappe, Ordre o)
        {
            Document d = new Document();
            d.Info.Title = "Faktura " + o.Onr;
            d.Info.Subject = "En faktura";
            d.Info.Author = "Kontormussen";

            OpretSide(d, o);

            MigraDoc.DocumentObjectModel.IO.DdlWriter.WriteToFile(d, "MigraDoc.mdddl");

            PdfDocumentRenderer renderer = new PdfDocumentRenderer(true, PdfSharp.Pdf.PdfFontEmbedding.Always);
            renderer.Document = d;
            renderer.RenderDocument();

            string filnavn = mappe + @"\Faktura-" + o.Onr + ".pdf";
            renderer.PdfDocument.Save(filnavn);
            //System.Diagnostics.Process.Start(filnavn);
        }
        public static string OpretAktuelMappe()
        {
            string nyMappe = string.Format(@"BackupFaktura\Faktura-{0:dd-MM-yyyy-hh-mm-ss}", DateTime.Now);

            System.IO.Directory.CreateDirectory(nyMappe);

            return nyMappe;
        }
        public static void OpretSide(Document d, Ordre o)
        {
            Section s = d.AddSection();
            //Indsæt logo
            Image logo = s.Headers.Primary.AddImage("scooterlandlogo.png");
            logo.Height = "2.5cm";
            logo.LockAspectRatio = true;
            logo.RelativeVertical = RelativeVertical.Line;
            logo.RelativeHorizontal = RelativeHorizontal.Margin;
            logo.Top = ShapePosition.Top;
            logo.Left = ShapePosition.Right;
            logo.WrapFormat.Style = WrapStyle.Through;

            //Indsæt fod
            Paragraph p = s.Footers.Primary.AddParagraph();
            p.AddText("Tlf: 72162616 * cvr.: 22113344 * Konto nr.: 1234 567890-0");
            p.AddLineBreak();
            p.AddText("Ved indbetaling via bank, påfør venligst fakturanummer");
            p.Format.Font.Size = 9;
            p.Format.Alignment = ParagraphAlignment.Center;

            // Opret frame til Scooterland data
            TextFrame sd = s.AddTextFrame();
            sd.Height = "3.0cm";
            sd.Width = "7.0cm";
            sd.Left = ShapePosition.Left;
            sd.RelativeHorizontal = RelativeHorizontal.Margin;
            sd.Top = "5.0cm";
            sd.RelativeVertical = RelativeVertical.Page;

            //Indsæt Scooterland data
            Paragraph sa = sd.AddParagraph("Scooterland * Boulevarden 48 * 7100 Vejle");
            sa.Format.Font.Name = "Arial";
            sa.Format.Font.Size = 7;
            sa.Format.SpaceAfter = 3;

            //Indsæt Kundeoplysninger
            Paragraph ko = sd.AddParagraph(o.AktuelKunde.Fornavn + " " + o.AktuelKunde.Efternavn);
            ko.AddLineBreak();
            ko.AddText(o.AktuelKunde.Adresse);
            ko.AddLineBreak();
            ko.AddText(o.AktuelKunde.Postnr + " " + o.AktuelKunde.Bynavn);
            ko.AddLineBreak();
            Paragraph knr = sd.AddParagraph("Kundenummer: " + o.AktuelKunde.Knr.ToString());
            knr.Format.SpaceBefore = "1cm";
            knr.Format.Font.Size = 7;
            knr.Format.Font.Bold = true;

            //Lav FakturaFelt
            Paragraph ff = s.AddParagraph();
            ff.Format.SpaceBefore = "8cm";
            ff.Format.Font.Size = 10;
            ff.Format.Font.Name = ("Arial");
            ff.AddFormattedText("FAKTURA nr: " + o.Onr, TextFormat.Bold);
            Paragraph da = s.AddParagraph();
            da.Format.Alignment = ParagraphAlignment.Right;
            da.Format.Font.Size = 7;
            da.AddText("Vejle den ");
            string dato = Convert.ToString(DateTime.Today.Day) + "/" + Convert.ToString(DateTime.Today.Month) + " - " + Convert.ToString(DateTime.Today.Year);
            da.AddText(dato);//AddDateField

            //opret tabel
            Table t = s.AddTable();
            t.Format.Font.Size = 7;
            t.Format.Font.Name = "Arial";
            t.Borders.Color = Colors.Black;
            t.Borders.Width = 0.25;
            t.Borders.Left.Width = 0.5;
            t.Borders.Right.Width = 0.5;
            t.Rows.LeftIndent = 0;

            //tabel kollonner
            Column c = t.AddColumn("1cm");
            c.Format.Alignment = ParagraphAlignment.Center;

            c = t.AddColumn("6cm");
            c.Format.Alignment = ParagraphAlignment.Left;

            c = t.AddColumn("3cm");
            c.Format.Alignment = ParagraphAlignment.Center;

            c = t.AddColumn("3cm");
            c.Format.Alignment = ParagraphAlignment.Center;

            c = t.AddColumn("3cm");
            c.Format.Alignment = ParagraphAlignment.Right;

            //tabel rækker
            Row r = t.AddRow();
            r.HeadingFormat = true;
            r.Format.Alignment = ParagraphAlignment.Center;
            r.Format.Font.Bold = true;
            r.Cells[0].AddParagraph("Antal");
            r.Cells[1].AddParagraph("Vare / Ydelser");
            r.Cells[2].AddParagraph("Udført");
            r.Cells[3].AddParagraph("Stk. pris");
            r.Cells[4].AddParagraph("Pris");
            t.SetEdge(0, 0, 5, 1, Edge.Box, BorderStyle.Single, 0.75);
            double sum = 0;
            foreach (OrdreLinie ol in o.OrdreLinier)
            {
                Row olr = r.Table.AddRow();
                olr.Cells[0].AddParagraph(ol.Antal.ToString());
                olr.Cells[1].AddParagraph(ol.VareIOrdreLinien.VareNavn);
                olr.Cells[2].AddParagraph(ol.UdfortDatoTid.ToString());
                olr.Cells[3].AddParagraph(ol.SolgtTilPris.ToString() + " kr");
                olr.Cells[4].AddParagraph((ol.SolgtTilPris * ol.Antal).ToString() + " kr");

                sum += ol.Antal * ol.SolgtTilPris;
            }

            if (o.Udlejning != null)
            {
                Row olr = r.Table.AddRow();
                olr.Cells[0].AddParagraph();
                olr.Cells[1].AddParagraph("Leje af scooter fra " + o.Udlejning.UdlejetStartDato.ToShortDateString() + " - " + o.Udlejning.UdlejetSlutDato.ToShortDateString());
                olr.Cells[2].AddParagraph();
                olr.Cells[3].AddParagraph();
                olr.Cells[4].AddParagraph();
                //km afregning
                Row ul2 = r.Table.AddRow();
                ul2.Cells[0].AddParagraph((o.Udlejning.SlutKortKm-o.Udlejning.StartKortKm).ToString());
                ul2.Cells[1].AddParagraph("Kørte kilometer");
                ul2.Cells[2].AddParagraph();
                ul2.Cells[3].AddParagraph(o.Udlejning.PrisPrKm.ToString());
                ul2.Cells[4].AddParagraph(((o.Udlejning.SlutKortKm-o.Udlejning.StartKortKm)*o.Udlejning.PrisPrKm).ToString());

                sum += (o.Udlejning.SlutKortKm-o.Udlejning.StartKortKm)*o.Udlejning.PrisPrKm;

                if (o.Udlejning.SelvRisikoBelob > 0)
                {
                    //km afregning
                    Row ul3 = r.Table.AddRow();
                    ul3.Cells[0].AddParagraph("1");
                    ul3.Cells[1].AddParagraph("Selvrisiko");
                    ul3.Cells[2].AddParagraph();
                    ul3.Cells[3].AddParagraph(o.Udlejning.SelvRisikoBelob.ToString());
                    ul3.Cells[4].AddParagraph(o.Udlejning.SelvRisikoBelob.ToString());

                    sum += o.Udlejning.SelvRisikoBelob;

                }

            }

            double moms = sum * 0.2;
            Row r2 = r.Table.AddRow();
            r2.Borders.Visible = false;
            r2 = r.Table.AddRow();
            r2.Cells[0].Borders.Visible = false;
            r2.Cells[1].Borders.Visible = false;
            r2.Cells[2].Borders.Visible = false;
            r2.Cells[3].AddParagraph("Totalpris:");
            r2.Cells[3].Borders.Visible = false;
            r2.Cells[3].Column.Format.Alignment = ParagraphAlignment.Right;
            r2.Cells[4].AddParagraph(sum.ToString() + " kr");
            r2.Cells[4].Format.Font.Underline = Underline.Dash;
            r2 = r.Table.AddRow();
            r2.Cells[0].Borders.Visible = false;
            r2.Cells[1].Borders.Visible = false;
            r2.Cells[2].Borders.Visible = false;
            r2.Cells[3].AddParagraph("Heraf moms:");
            r2.Cells[3].Borders.Visible = false;
            r2.Cells[3].Column.Format.Alignment = ParagraphAlignment.Right;
            r2.Cells[4].AddParagraph(moms.ToString() + " kr");
        }

        public static void SamleOgUdskrivFaktura(string mappenavn)
        {

            DirectoryInfo di = new DirectoryInfo(mappenavn);
            FileInfo[] filliste = di.GetFiles();

            PdfDocument henteFiler = new PdfDocument();

            foreach (FileInfo file in filliste)
            {
                PdfDocument pdffil = PdfReader.Open(mappenavn + @"\" + file.Name, PdfDocumentOpenMode.Import);

                int count = pdffil.PageCount;
                for (int idx = 0; idx < count; idx++)
                {
                    PdfPage side = pdffil.Pages[idx];
                    henteFiler.AddPage(side);
                }
            }
            string nyfil = mappenavn + "/AlleFaktura.pdf";
            henteFiler.Save(nyfil);
            Process.Start(nyfil);

        } 
        

        public static void GemKundeSidsteBesøgsRapport(string sti, List<Kunde> liste, DateTime dato)
        {
            StreamWriter w = new StreamWriter(sti);
            
            w.WriteLine("Oversigt over kunder hvis Scootere ikke har været på værksted siden " + dato.ToShortDateString());
            w.WriteLine();
            w.WriteLine("Fornavn".PadRight(10)+"Efternavn".PadRight(15)+"Adresse".PadRight(20)+"Postnr".PadRight(8)+"By".PadRight(15)+"Reg.nr".PadRight(8)+"Mærke".PadRight(8)+"Model".PadRight(20)+"Servicedato".PadRight(10));
            for (int i = 0; i < 114; i++)
            {
                w.Write("_");
            }
            w.WriteLine();
            foreach (Kunde k in liste)
            {
                string s = k.Fornavn.PadRight(10) + k.Efternavn.PadRight(15) + k.Adresse.PadRight(20) + k.Postnr.ToString().PadRight(8) + k.Bynavn.PadRight(15) + k.Scootere.ElementAt(0).Key.PadRight(8) + k.Scootere.ElementAt(0).Value.Certifikat.CertNavn.PadRight(8) + k.Scootere.ElementAt(0).Value.Model.PadRight(20) + k.Scootere.ElementAt(0).Value.SidsteService.ToShortDateString().PadRight(10);

                w.WriteLine(s);

            }
            for (int i = 0; i < 114; i++)
            {
                w.Write("_");
            }
            w.Close();
        }
    }
}
